package club.monkeywood.service.buyer.impl;

import club.monkeywood.dto.buyer.RatingDTO;
import club.monkeywood.dataobject.Production;
import club.monkeywood.dataobject.ProductionCategory;
import club.monkeywood.repository.ProductCategoryRepository;
import club.monkeywood.service.buyer.ProductionCategoryService;
import club.monkeywood.service.buyer.ProductionInfoService;
import club.monkeywood.service.buyer.RatingService;
import club.monkeywood.vo.buyer.ProductionRatingVO;
import club.monkeywood.vo.buyer.ProductionCategoryVO;
import club.monkeywood.vo.buyer.ProductionInfoVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ProductionCategoryServiceImpl implements ProductionCategoryService {

    @Autowired
    private ProductCategoryRepository repository;

    @Autowired
    private ProductionInfoService productionInfoService;

    @Autowired
    private RatingService ratingService;

    @Override
    public ProductionCategory findById(Integer id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public List<ProductionCategory> findByIdIn(List<Integer> ids) {
        return repository.findByIdIn(ids);
    }

    @Override
    public List<ProductionCategory> findByName(String name) {
        return repository.findByName(name);
    }

    @Override
    public List<ProductionCategory> findAll() {
        return repository.findAll();
    }

    @Override
    public ProductionCategory save(ProductionCategory productionCategory) {
        return repository.save(productionCategory);
    }

    @Override
    public List<ProductionCategoryVO> getProductions(Integer sellerId) {
        //查询所有上架商品
        List<Production> productionList = productionInfoService.findUPAll();

        //从商品列表中提取商品分类数据集
        List<Integer> productionCategoryIds = productionList.stream()
                .map(e -> e.getCategoryId())
                .distinct()
                .collect(Collectors.toList());
        //查询商品分类列表
        List<ProductionCategory> productionCategoryList = findByIdIn(productionCategoryIds);

        //查询所有评论
        List<RatingDTO> ratingList = ratingService.queryProductionRatingBySellerId(sellerId);

        //拼装按商品类型分组的商品列表
        //遍历商品分类list
        List<ProductionCategoryVO> productionCategoryVOList = productionCategoryList.stream()
                .map(category -> {
                    //创建商品分类VO对象
                    ProductionCategoryVO productionCategoryVO = new ProductionCategoryVO();
                    //复制基本信息
                    productionCategoryVO.setName(category.getName());
                    productionCategoryVO.setCategoryId(category.getId());

                    //拼装商品VO列表
                    List<ProductionInfoVO> productionInfoVOs = productionList.stream()
                            .filter(production -> {
                                return production.getCategoryId().equals(category.getId());
                            })
                            .map(item -> {
                                ProductionInfoVO productionInfoVO = new ProductionInfoVO();
                                BeanUtils.copyProperties(item, productionInfoVO);

                                //拼装商品评价列表
                                //过滤出商品的评论dto列表
                                List<ProductionRatingVO> ratingVOs = ratingList.stream()
                                        .filter(r -> r.getProductionId().equals(item.getId()))
                                        .map(r -> {
                                            ProductionRatingVO productionRatingVO = new ProductionRatingVO();
                                            //dto转vo
                                            BeanUtils.copyProperties(r, productionRatingVO);
                                            return productionRatingVO;
                                        })
                                        .collect(Collectors.toList());

                                productionInfoVO.setRatings(ratingVOs);
                                return productionInfoVO;
                            })
                            .collect(Collectors.toList());

                    //装配商品列表到商品分类VO对象上
                    productionCategoryVO.setProductionInfoVOS(productionInfoVOs);

                    //返回装配好的商品分类VO对象
                    return productionCategoryVO;
                })
                //将map中返回的商品分类VO对象收集，组装成list
                .collect(Collectors.toList());

        return productionCategoryVOList;
    }
}
